import mainRoot from "../../Game/MainRoot";
import gameConfig from "../configs/gameConfig";
import O1HD, { modules, services } from "../gameEngine";
import { REWARD_TYPE } from "../services/userContent/userInterface";

const {ccclass, property} = cc._decorator;

/**添加快捷方式 桌面 */
@ccclass
export default class addDestok extends cc.Component {

    @property({type:cc.Node,displayName:"快捷方式按钮"})
    private ShortcutBtn:cc.Node=null;

    @property({type:cc.Node,tooltip:"箭头节点"})
    arrowNode:cc.Node = null;

    private readonly  FirstKey:string="get-First-Add";

    /**最后一次进入的日期 日 */
    private readonly  TimeDay:string="get-End-timeDay";

    private tt :any = null

    arrowAni(){
        let tween = cc.tween(this.arrowNode).by(1,{y:20}).by(1,{y:-20});
        cc.tween(this.arrowNode).repeatForever(tween).start();
    }

    hideArrow(){
        this.arrowNode.active = false;
    }

    onLoad () {
        this.tt =window['tt']
        this.ShortcutBtn.on(cc.Node.EventType.TOUCH_END,this.onBtnMe,this)
        let that = this
        console.info("添加快捷方式 onBtnMe", this.tt)
        this.arrowAni();
        this.hideArrow();
        if( this.tt){
            //字节跳动 字节跳动lite 和西瓜拒审 关闭
            if(!mainRoot.isShortcutOpen()) {
                this.node.active = false
                this.hideArrow();
                return
            }
            this.tt.onShow((res) => {
                console.log("启动参数如下：", res);
                console.log("启动参数如下：", res.query);
                console.log("来源信息如下：", res.refererInfo);
               
                
              });

            this.check()  

            this.tt.checkShortcut({
                success: function (res) {
                    console.info("success快捷方式",res)
                    if(res.status.exist){
                        that.node.active = false
                        console.info("success快捷方式存在")
                    }else{
                        console.info("success快捷方式不存在")
                    }
                    //获取快捷方式是否存在 res.status.exist
                },
                fail: function (res) {
                
                    console.info("fail快捷方式检查onLoad",  res.errMsg)
                  

                }
            })
        }else{
            // this.node.active = false

        }
    }


    onBtnMe(){
        console.info("添加快捷方式 onBtnMe")
        // O1HD.getModules().uiControlModule.showMessageBox("添加失败");
        let self=this
        if( this.tt){
            console.info("添加快捷方式")
            O1HD.getServices().miniGameSrv.getAppApiInstance().reportAnalytics('addDestok',{addDestok:"主界面点击添加桌面"} );
            this.tt.addShortcut({
                success: function (res) {
                    self.node.active = false;
                    self.hideArrow();
                    let key=O1HD.getServices().localStorageSrv.getStorage(self.FirstKey);
                    if(key&&Number(key)==1)
                    {
                        console.info("已经添加过快捷方式")
                    }else
                    {
                        console.info("添加快捷方式成功")
                        self.addRewarded()
                        O1HD.getServices().localStorageSrv.setStorage(self.FirstKey,1);
                        O1HD.getServices().miniGameSrv.getAppApiInstance().reportAnalytics('addDestok',{addDestok:"主界面点击添加桌面成功"} );
                    }
                },
                fail: function (res) {
                    console.info("添加快捷方式失败",res)
                    O1HD.getServices().miniGameSrv.getAppApiInstance().reportAnalytics('addDestok',{addDestok:"主界面点击添加桌面失败"} );
                    O1HD.getModules().uiControlModule.showMessageBox("添加失败");
                }
            })
        }else{
            this.addRewarded()
            O1HD.getServices().localStorageSrv.setStorage(self.FirstKey,1);
        }
    }

    start () {
            //弹窗出来的 添加 打点一次  

          
    }

    //从桌面进打点        
    check(){
        //刷新时间 为0点
  
        let options  = this.tt.getLaunchOptionsSync();
        console.info("checkoptions",options)
        if(options.scene == "021020" ) //抖音桌面图标
        { 
            console.info("check")
            O1HD.getServices().miniGameSrv.getAppApiInstance().reportAnalytics('addDestok',{addDestok:"抖音桌面进入"} );


            let curTime = new Date().getDate()
            console.info("checkcurTime",curTime)
            let key=O1HD.getServices().localStorageSrv.getStorage(this.TimeDay);
            if(key){  //
                if(Number(key)==curTime){  //当天重复进 不给奖励
    
                    console.info("checkcurTime 不给奖励",curTime)
                }else{ //刷新  给奖励
                    console.info("checkcurTime 给奖励",curTime)
                    this.addRewarded()
                    O1HD.getServices().localStorageSrv.setStorage(this.TimeDay,curTime);
                }
    
            }else{ //第一次进  给奖励
                console.info("checkcurTime  第一次进给奖励",curTime)
                this.addRewarded()
                O1HD.getServices().localStorageSrv.setStorage(this.TimeDay,curTime);
            }
        }
    }

    addRewarded(){
        // services().userSrv.addPowerOfVideo();
        // O1HD.getServices().userSrv.setCoins(1,true);
        // modules().uiControlModule.showGetRewarded(REWARD_TYPE.COIN,1,"恭喜");
    }



    // update (dt) {}
}
